<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <link href="/css/enroll.css" rel="stylesheet" type="text/css" />
    <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="/js/register.js"></script>


</head>

<body>

    <div id="hr" v-cloak>
        <div class="login">
            <div class="font">
                <h2>用户注册</h2>
            </div>
            <div class="jq22-register-form" id="verifyCheck">
                <form action="">
                    <div class="jq22-register-form-item-zhuce">
                        <input type="text" placeholder="请输入验证码" id="randomCode"
                            style="height:43px;position: relative; top:-15px; font-size:16px;"
                            class="txt02 f-r3 f-fl required" data-valid="isNonEmpty"
                            data-error="<i class='icon-tips'></i>请输入验证码" id="text">
                        <label class="focus valid"></label>
                        <canvas id="canvas" width="90" height="40" onclick="dj()" style="border: 1px solid rgb(148, 148, 148);
                   border-radius: 5px;"></canvas>
                        <!-- <button class="btn" onclick="sublim()">提交</button> -->
                    </div>

                    <div class="jq22-register-form-item">
                        <input type="text" name="phone" placeholder="手机号码" class="txt01 f-r3 required" keycodes="tel"
                            tabindex="1" data-valid="isNonEmpty||isPhone"
                            data-error="<i class='icon-tips'></i>请输入手机号||<i class='icon-tips'></i>手机号码格式不正确"
                            maxlength="11" id="phone">
                        <label class="focus valid">
                            <div class="msg" style="display:none"><i class='icon-tips'></i>您还未输入手机号</div>
                        </label>
                        <span class="jq22-get-code btn btn-gray f-r3 f-ml5 f-size13" id="time_box" disabled
                            style="display:none;"></span>
                        <span class="jq22-get-code btn btn-gray f-r3 f-ml5 f-size13" id="verifyYz">获取验证码</span>
                    </div>


                    <div class="jq22-register-form-item">
                        <input type="text" placeholder="验证码" maxlength="6" id="verifyNo"
                            class="txt02 f-r3 f-fl required" data-valid="isNonEmpty||isInt"
                            data-error="<i class='icon-tips'></i>请填写正确的手机验证码||<i class='icon-tips'></i>请填写6位手机验证码">
                        <label class="focus valid"></label>
                    </div>
                    <!-- <div class="jq22-register-form-item">
                    <input type="text" name="username" maxlength="20" placeholder="账户名" class="txt03 f-r3 required"
                        tabindex="3" data-valid="isNonEmpty||between:3-20||isUname"
                        data-error="<i class='icon-tips'></i>您还没有输入账户名||<i class='icon-tips'></i>用户名长度3-20位||<i class='icon-tips'></i>只能输入字母、数字、且以中文或字母开头"
                        id="adminNo">
                    <label class="focus valid"></label>
                </div> -->
                    <div class="jq22-register-form-item">
                        <input type="password" name="password" placeholder="设置密码" id="password" maxlength="20"
                            class="txt04 f-r3 required" tabindex="4" style="ime-mode:disabled;" onpaste="return  false"
                            autocomplete="off" data-valid="isNonEmpty||between:6-20||level:2"
                            data-error="<i class='icon-tips'></i>密码太短，最少6位||<i class='icon-tips'></i>密码长度6-20位">
                        <label class="focus valid"></label>
                    </div>
                    <div class="jq22-register-form-item">
                        <input type="password" name="password" placeholder="确认密码" maxlength="20"
                            class="txt05 f-r3 required" tabindex="5" style="ime-mode:disabled;" onpaste="return  false"
                            autocomplete="off" data-valid="isNonEmpty||between:6-16||isRepeat:password"
                            data-error="<i class='icon-tips'></i>密码太短，最少6位||<i class='icon-tips'></i>密码长度6-16位||<i class='icon-tips'></i>两次密码输入不一致"
                            id="rePassword">
                        <label class="focus valid"></label>
                    </div>
                    <div class="jq22-register-form-item">
                        <!-- <div class="protocol">注册即同意<a href="#">《用户使用协议》</a>&amp;<a href="#">《隐私权条款》</a></div> -->
                        <input id="jq22-btn-reg" class="jq22-btn-reg" placeholder="" readonly="readonly"
                            onclick="sublim()" value="注册">
                    </div>
                </form>
            </div>
            <!-- <div class="btn">
            <button class="login_btn" onclick="login()">注 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;册</button>
        </div> -->
        </div>
    </div>
</body>

<script>
    var show_num = [];
    draw(show_num);
    function dj() {
        draw(show_num);
    }
    // function sublim() {
    //     var val = document.getElementById("text").value;
    //     var num = show_num.join("");
    //     if (val == '') {
    //         alert('请输入验证码！');
    //     } else if (val == num) {
    //         alert('提交成功！');
    //         document.getElementById(".input-val").val('');
    //         draw(show_num);

    //     } else {
    //         alert('验证码错误！\n你输入的是:  ' + val + "\n正确的是:  " + num + '\n请重新输入！');
    //         document.getElementById("text").value = '';
    //         draw(show_num);
    //     }



    // }
    function draw(show_num) {
        var canvas_width = document.getElementById('canvas').clientWidth;
        var canvas_height = document.getElementById('canvas').clientHeight;
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width;
        canvas.height = canvas_height;
        var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
        var aCode = sCode.split(",");
        var aLength = aCode.length;//获取到数组的长度

        for (var i = 0; i <= 3; i++) {
            var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            var txt = aCode[j];//得到随机的一个内容
            show_num[i] = txt;
            console.log(show_num, 'show_numshow_numshow_num')
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";
            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }
    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    function checkPhone(Phone) {
        var reg = '^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-9])|(18[0-9])|166|198|199|191|(147))\\d{8}$';
        var regExp = new RegExp(reg);
        return regExp.test(Phone)
    }

    function testCode() {
        let code = document.getElementById('randomCode').value
        let showNum = show_num.join('')
        if (code.toUpperCase() == showNum.toUpperCase()) return true
        else return false
    }
    //注册
    let sendCodeBtn = document.getElementById('verifyYz')
    sendCodeBtn.onclick = () => {
        let Phone = document.getElementById("phone").value
        if (!testCode()) {
            alert('请检查您的图片验证码是否正确')
            return
        }
        if (!checkPhone(Phone)) {
            alert('请填写正确的手机号')
            return
        }
        sendCode('post', "http://119.3.210.33:7002/Base_Manage/Base_User/SaveRegister", { Phone })
    }

    function sendCode(method, url, data) {
        //1.创建对象
        var xhr;
        xhr = new XMLHttpRequest();
        //3.监听服务器响应
        xhr.onreadystatechange = function () {
            if (xhr.responseText) {
                if (xhr.status != 200) {
                    alert('网络连接失败!请重试')
                    return
                }
                console.log(xhr.responseText.indexOf('"Success":false'));
                if (xhr.responseText.indexOf('"Success":false') !== -1) {
                    let arr = xhr.responseText.split(',')
                    let msg = arr[arr.length - 1].split('"')
                    alert(msg[msg.length - 2])
                } else {
                    alert('短信发送成功')
                }
            }
        };
        //2.打开链接
        xhr.open(
            method,
            url,
            false
        );
        //3.5 请求头（POST请求时设置）
        xhr.setRequestHeader("Content-type", "application/json");
        //4.发送请求
        xhr.send(JSON.stringify(data));
    }



    //保存
    let subCodeBtn = document.getElementById('jq22-btn-reg')
    subCodeBtn.onclick = () => {
        let Phone = document.getElementById("phone").value
        let Password = document.getElementById("rePassword").value
        let SmsCode = document.getElementById("verifyNo").value
        console.log(Phone, Password, SmsCode);
        if (!testCode()) {
            alert('请检查您的图片验证码是否正确')
            return
        }
        if (!checkPhone(Phone) || !Password || !SmsCode) {
            alert('请完善注册信息')
            return
        }
        submit('post', "http://119.3.210.33:7002/Base_Manage/Base_User/SaveUserModel", {
            "UserPhone": Phone,
            "Password": Password,
            "SmsCode": SmsCode,
        })
    }

    function submit(method, url, data) {
        //1.创建对象
        var xhr;
        xhr = new XMLHttpRequest();
        //3.监听服务器响应
        xhr.onreadystatechange = function () {
            console.log(xhr.responseText);
            console.log(xhr.status);
            if (xhr.responseText) {
                if (xhr.status != 200) {
                    alert('网络连接失败!请重试')
                    return
                }
                console.log(xhr.responseText.indexOf('"Success":false'));
                if (xhr.responseText.indexOf('"Success":false') !== -1) {
                    let arr = xhr.responseText.split(',')
                    let msg = arr[arr.length - 1].split('"')
                    alert(msg[msg.length - 2])
                } else {
                    alert('成功')
                }
            }
        };
        //2.打开链接
        xhr.open(
            method,
            url,
            false
        );
        //3.5 请求头（POST请求时设置）
        xhr.setRequestHeader("Content-type", "application/json");
        //4.发送请求
        xhr.send(JSON.stringify(data));
    }
</script>

</html>